package com.topvision.auth.config;

import com.topvision.auth.security.CustomAuthenticationProvider;
import com.topvision.auth.security.DomainUserDetailsService;
import com.topvision.common.token.TokenProvider;
import com.topvision.config.Http401UnauthorizedEntryPoint;
import com.topvision.config.JWTConfiguration;
import io.swagger.models.HttpMethod;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.data.repository.query.SecurityEvaluationContextExtension;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.filter.CorsFilter;

import javax.inject.Inject;

/**
 * Created by wangyunfei on 2017/6/9.
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  /*  @Inject
    private CorsFilter corsFilter;*/

    @Inject
    private TokenProvider tokenProvider;


    @Inject
    private CustomAuthenticationProvider customAuthenticationProvider;
  //  @Inject
    private CorsFilter corsFilter;

    @Bean
    @Override
    public UserDetailsService userDetailsService(){
        return new DomainUserDetailsService();
    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(customAuthenticationProvider).userDetailsService(userDetailsService())
                .passwordEncoder(passwordEncoder());
    }

    @Bean
    public SecurityEvaluationContextExtension securityEvaluationContextExtension() {
        return new SecurityEvaluationContextExtension();
    }

    //不定义没有password grant_type
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
              //  .addFilterBefore(corsFilter, UsernamePasswordAuthenticationFilter.class)
                .exceptionHandling()
                .authenticationEntryPoint(new Http401UnauthorizedEntryPoint())
                .and()
                .csrf()
                .disable()
                .headers()
                .frameOptions()
                .disable()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
              //  .antMatchers("/api" + BaseConstants.API_PREFIX_PROTECTED + "/**").authenticated()
               // .antMatchers("/api" + BaseConstants.API_PREFIX + "/**").permitAll()
                .antMatchers("/api/**").authenticated()
                .antMatchers("/management/health").permitAll()
                .antMatchers("/management/**").hasAuthority("role_admin")
                .antMatchers("/v2/api-docs/**").permitAll()
                /*.antMatchers("/swagger-resources/configuration/ui").permitAll()
                .antMatchers("/swagger-ui.html").permitAll()*/
                .antMatchers("/info").permitAll()
                .antMatchers(org.springframework.http.HttpMethod.OPTIONS, "/oauth/token").permitAll()
               /* .and()
                .apply(new JWTConfiguration(tokenProvider))*/;
    }


}
